home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15129 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.6 KB  |  47 lines

  1. Newsgroups: comp.lang.c++
  2. Path: undergrad.math.uwaterloo.ca!sckettle
  3. From: sckettle@undergrad.math.uwaterloo.ca (Steve Kettle)
  4. Subject: Re: Is it OK to delete const *type pointers?
  5. Sender: news@undergrad.math.uwaterloo.ca (news spool owner)
  6. Message-ID: <DpAo8B.82s@undergrad.math.uwaterloo.ca>
  7. Date: Wed, 3 Apr 1996 16:24:10 GMT
  8. References: <4jhjub$fpc@mag1.magmacom.com>
  9. Nntp-Posting-Host: noether.math.uwaterloo.ca
  10. Organization: University of Waterloo
  11.  
  12. In article <4jhjub$fpc@mag1.magmacom.com>,
  13. Acme Instant Dehydrated Boulder Kit <ezust@mag1.magmacom.com> wrote:
  14. >
  15. >Some compilers let me do this, others do not. What I would like to know is,
  16. >what does Stroustrup think? I.e. is it written somewhere in the ARM (I checked
  17. >but can't find it) or in the draft standard? If somoene could e-mail me a
  18. >quote or a pointer to a place where I can read where it says such a thing
  19. >should or should not be allowed, I would appreciate it!
  20. >
  21. >const int* array= new int[30];
  22. >delete[] array;
  23. >
  24. >MSVC says "can not delete const*"
  25. >Some versions of G++ also don't let me do this.
  26. >
  27.  
  28. I don't think you should be allowed to delete a pointer to const memory.
  29.  
  30. eg.
  31. main()
  32. {
  33.    const int * temp;
  34.    temp = new int;
  35.    delete temp;  // not allowed.
  36. }
  37.  
  38. Think of the problems if you could delete the pointer.  What if 
  39. it pointed to read only memory - you delete and memory fault 
  40. ( or soon to be memory fault ).  When you have const int * temp
  41. basically this means the compiler will do every thing in its 
  42. power to prevent you from modifying what temp points to, Unless
  43. you cast away the const or get at the memory some other way that
  44. is undetectable at compile time.  
  45.  
  46. -- 
  47.